﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Indigo.Quibbler.Data;

namespace Indigo.Quibbler.Controls
{
    /// <summary>
    /// Interaction logic for AdsControl.xaml
    /// </summary>
    public partial class SearchControl : UserControl
    {
        #region Public Properties
        private List<NewsArticle> _itemSource;
        public List<NewsArticle> ItemSource
        {
            get { return _itemSource; }
            set
            {
                if (value != null)
                {
                    _itemSource = value;
                    _SetItems();
                    textNoResult.Visibility = Visibility.Collapsed;
                    searchResults.Visibility = Visibility.Visible;
                }
                else
                {
                    textNoResult.Visibility = Visibility.Visible;
                    searchResults.Visibility = Visibility.Collapsed;
                }
            }
        }
        #endregion

        #region Delegates
        public delegate void ArticleSelectedEventHandler(object sender, NewsArticle parameters);
        #endregion

        #region Public Events
        public event ArticleSelectedEventHandler ArticleSelected;
        #endregion

        #region Ctor
        public SearchControl()
        {
            InitializeComponent();
            searchResults.ArticleSelectedEvent += new SearchResultsControl.ArticleSelectedEventHandler(_searchResults_ArticleSelectedEvent);
        }
        #endregion

        #region Helper Functions
        private void _SetItems()
        {
            searchResults.ItemSource = _itemSource;
        }
        #endregion

        #region Event Handlers
        void _searchResults_ArticleSelectedEvent(object sender, int selectedIndex)
        {
            if (selectedIndex >= 0 && selectedIndex < _itemSource.Count)
            {
                ArticleSelected(this, _itemSource[selectedIndex]);
            }

        }
        #endregion
    }
}
